﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace Client
{
    public partial class FormDeviceManager : BaseChildForm
    {
        QunYing.BLL.ICSA_Device bll_device = new QunYing.BLL.ICSA_Device();
        QunYing.BLL.VipInfo bll_VipInfo = new QunYing.BLL.VipInfo();
         
        public FormDeviceManager()
        {
            InitializeComponent();
            this.sharpGrid1.RowDoubleClickEvent += new EventHandler<Spore.Controls.RowDoubleClickEventArg>(sharpGrid1_RowDoubleClickEvent);
            BindData();
            this.SizeChanged += new EventHandler(FormDeviceManager_SizeChanged);
            this.sharpGrid1.AllowPopupMenu = true;
            this.sharpGrid1.AddPopupMenu(new ToolStripSeparator(), null);
            this.sharpGrid1.AddPopupMenu(new ToolStripMenuItem("设备图表信息"), this.LookTuBiao);
        }

       
        private void LookTuBiao(object sender, EventArgs e)
        {
            DataRow dr = this.GetICSAInfo();
            if (dr == null)
            {
                GlobalInfo.MsgBoxShowError("请选择一个设备！");
                return;
            }

            DataTable dt = dr.Table.Clone();
            dt.ImportRow(dr);
            var vvv = bll_device.DataTableToList(dt)[0];
            FormICSAChart fic = new FormICSAChart();
            fic.BindData(vvv);
            fic.ShowDialog();
        }

        void FormDeviceManager_SizeChanged(object sender, EventArgs e)
        {
            if (!this.DesignMode && this.panel_btn != null)
            {
                if (this.panel_btn.Parent != null)
                    this.panel_btn.Left = (this.panel_btn.Parent.Width - this.panel_btn.Width) / 2;
            }
        }

        void sharpGrid1_RowDoubleClickEvent(object sender, Spore.Controls.RowDoubleClickEventArg e)
        {
            DataRow row = this.GetICSAInfo();
            if (row == null)
            {
                GlobalInfo.MsgBoxShowError("请选择一台设备");
                return;
            }
            var v = this.bll_device.GetModel(row["ICSA设备ID"].ToString());
            if (v != null)
            {
                List<QunYing.Model.ICSA_Device> model = new List<QunYing.Model.ICSA_Device>() { v };
                FormICSALook form = SingleObjManager.GetSingleObj<FormICSALook>();
                form.BindData(model);
                form.ShowDialog();
            }
          
        }

        private DataRow GetICSAInfo()
        {
            GridView view = this.sharpGrid1.MainView as GridView;
            int[] rows = view.GetSelectedRows();
            if (rows.Length == 0)
                return null;
            foreach (var v in rows)
            {
                if (v < 0)
                    continue;
                DataRowView rowView = view.GetRow(v) as DataRowView;
                if (rowView == null)
                    return null;
                return rowView.Row;
            }
            return null;

        }

        public override void BindData()
        {
            if (GlobalInfo.UserLoginInfo.角色 == "平台管理员")
            {
                this.sharpGrid1.DataSource = bll_device.GetAllList().Tables[0];
            }
            else if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                this.sharpGrid1.DataSource = bll_device.GetList(string.Format("所属经销商 = '{0}'",GlobalInfo.UserLoginInfo.用户ID)).Tables[0];
                this.panelControl1.Visible = false;
            }
            ((GridView)this.sharpGrid1.MainView).Columns["定时器1开启时间"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["定时器1关闭时间"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["定时器2开启时间"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["定时器2关闭时间"].Visible = false;
            ((GridView)this.sharpGrid1.MainView).Columns["定时上报时间"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            ((GridView)this.sharpGrid1.MainView).Columns["定时上报时间"].DisplayFormat.FormatString = "HH:mm:ss";
            ((GridView)this.sharpGrid1.MainView).Columns["ICSA设备ID"].Visible = false;
            

        }

        private void btn_新增设备_Click(object sender, EventArgs e)
        {
            FormICSAdd form = SingleObjManager.GetSingleObj<FormICSAdd>();
            form.ShowDialog();
            this.BindData();
        }
    }
}